package io.mvvm.ws.utils;

import com.alibaba.fastjson.JSON;
import io.mvvm.ws.WebSocketChannelHolder;
import io.mvvm.ws.entity.ResponseInfo;
import io.mvvm.ws.entity.SendMessageEntity;
import io.mvvm.ws.enumerate.SendMessageTypeEnum;

import java.util.Collections;
import java.util.List;

/**
 * @program: seize-login
 * @description: 消息发送
 * @author: 潘
 * @create: 2021-05-16 12:25
 **/
public class SendMessageUtil {

    public static boolean send(SendMessageEntity entity) {
        SendMessageTypeEnum messageType = entity.getMessageType();
        ResponseInfo build = ResponseInfo.builder()
                .action(messageType.getAction())
                .msg(messageType.getMessage())
                .params(entity.getParams())
                .build();
        switch (messageType.getCode()) {
            case 100:
                // 发送下线消息
                // 做一些操作
                break;
        }
        return send(entity.getUserId(), build);
    }

    public static boolean send(List<String> userId, ResponseInfo responseInfo) {
        return WebSocketChannelHolder.sendMsg(userId, JSON.toJSONString(responseInfo));
    }
    public static boolean send(String userId, ResponseInfo responseInfo) {
        return send(Collections.singletonList(userId), responseInfo);
    }

}
